<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/30
 * Time: 10:26
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use app\admin\model\Banner as BannerModel;
class Banner extends Common
{
    /*
     * 列表展示
     */
    public function banner()
    {
        //获取数据库的信息用于展示
        if (Request::instance()->isAjax()) {
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_del'] = array('eq',0);
            $banner = new BannerModel();
            $count = $banner->getBannerCount($where);
            $data = $banner->getBannerList($where,$startrow,$pagesize);
            ajax_return(config('code.success_show'),'请求成功',$data,$count);

        } else {
            return view();
        }
    }
    /*
     * pc 轮播图页面展示
     */
    public function bannerpc()
    {
        if(Request::instance()->isAjax()){
            $pagedata = Request::instance()->get();
            $pagesize = $pagedata['limit'];
            $page = (int)$pagedata['page'];
            if ($page == 1) {
                $startrow = 0;
            } else {
                $startrow = ($page - 1) * $pagesize;
            }
            $where['is_del'] = array('eq',0);
            $banner = new BannerModel();
            $count = $banner->getBannerpcCount($where);
            $data = $banner->getBannerpcList($where,$startrow,$pagesize);
            ajax_return(config('code.success_show'),'请求成功',$data,$count);
        } else {
            return view();
        }
    }
    /*
     * 编辑单元格
     */
    public function editSort()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('banner')->where('banner_id', $post['id'])->setField('banner_sort', $post['sort']);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => '修改失败'];
            }
        }
    }
    /*
     * 修改单元格
     */
    public function editSortPc()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $res = Db::name('banner_pc')->where('banner_id', $post['id'])->setField('banner_sort', $post['sort']);
            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => '修改失败'];
            }
        }
    }
    /*
     * 删除
     */
    public function bannerDel()
    {
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $res = Db::name('banner')->where('banner_id',$post['id'])->setField('is_del',1);
            if($res !== false){
                return "1";
            }else{
                return "2";
            }
        }
    }
    /*
     * pc 删除
     */
    public function bannerPcDel()
    {
        if (Request::instance()->isAjax()){
            $post = Request::instance()->post();
            $res = Db::name('banner_pc')->where('banner_id',$post['id'])->setField('is_del',1);
            if($res !== false){
                return "1";
            }else{
                return "2";
            }
        }
    }
    /*
     * 是否使用
     */
    public function editIsShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'is_display' => $post['is_show'],
            );

            $res = Db::name('banner')->where('banner_id', $post['id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }
    /*
     * 是否使用 pc
     */
    public function editPcIsShow()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $saveData = array(
                'is_display' => $post['is_show'],
            );

            $res = Db::name('banner_pc')->where('banner_id', $post['id'])->update($saveData);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 1];
            }
        }
    }

    /*
     * 添加轮播图
     */
    public function bannerAdd()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];


            if (!$post['banner_img']) {
                return ['code' => 1, 'msg' => '请上传广告图片'];
                exit;
            }

            if (!$post['banner_sort']) {
                return ['code' => 1, 'msg' => '请设置权重'];
                exit;
            }
            if (!$post['banner_link']) {
                return ['code' => 1, 'msg' => '请输入链接'];
                exit;
            }

            //存入到数组中
            $data = [
                'banner_img' => $post['banner_img'],
                'banner_link' => $post['banner_link'],
                'banner_sort' => $post['banner_sort'],
                'create_time' => time()
            ];
            $res = Db::name('banner')->insert($data);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        }
    }
    /*
     * 添加广告 pc
     */
    public function bannerPcAdd()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];


            if (!$post['banner_img']) {
                return ['code' => 1, 'msg' => '请上传广告图片'];
                exit;
            }

            if (!$post['banner_sort']) {
                return ['code' => 1, 'msg' => '请设置权重'];
                exit;
            }
            if (!$post['banner_link']) {
                return ['code' => 1, 'msg' => '请输入链接'];
                exit;
            }

            //存入到数组中
            $data = [
                'banner_img' => $post['banner_img'],
                'banner_link' => $post['banner_link'],
                'banner_sort' => $post['banner_sort'],
                'create_time' => time()
            ];
            $res = Db::name('banner_pc')->insert($data);
            if ($res) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        }
    }
    /**
     * 广告位修改*
     */
    public function editBanner()
    {
        //获取id
        $id = $_GET['banner_id'];
        Session::set('banner_id', $id);
        //查询数据库
        $data = Db::name('banner')
            ->where('banner_id', $id)
            ->find();
        if ($data !== false) {
            $this->assign('data', $data);
        }
        return $this->fetch();
    }
    /*
     * pc 修改广告
     */
    public function editBannerpc()
    {
        //获取id
        $id = $_GET['banner_id'];
        Session::set('banner_id', $id);
        //查询数据库
        $data = Db::name('banner_pc')
            ->where('banner_id', $id)
            ->find();
        if ($data !== false) {
            $this->assign('data', $data);
        }
        return $this->fetch();
    }
    /**
     * 确认修改广告位*
     */
    public function editXiu()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            if (!$post['banner_img']) {
                return ['code' => 1, 'msg' => '请上传轮播图'];
            }
            //存入到数组中
            $data = [
                'banner_img' => $post['banner_img'],
                'banner_link' => $post['banner_link'],
                'banner_sort' => $post['banner_sort'],
                'create_time' => time(),
            ];

            $res = Db::name('banner')->where('banner_id', Session::get('banner_id'))->update($data);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        }
    }

    /*
     * 确认修改广告图
     */
    public function editPcXiu()
    {
        if (Request::instance()->isAjax()) {
            $post = Request::instance()->post();
            $post = $post['data'];
            if (!$post['banner_img']) {
                return ['code' => 1, 'msg' => '请上传轮播图'];
            }
            //存入到数组中
            $data = [
                'banner_img' => $post['banner_img'],
                'banner_link' => $post['banner_link'],
                'banner_sort' => $post['banner_sort'],
                'create_time' => time(),
            ];

            $res = Db::name('banner_pc')->where('banner_id', Session::get('banner_id'))->update($data);

            if ($res !== false) {
                return ['code' => 0];
            } else {
                return ['code' => 2];
            }
        }
    }

}